package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConst;
import com.itheima.dao.MemberDao;
import com.itheima.entity.Result;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Member;
import org.jacoco.agent.rt.internal_43f5073.core.internal.flow.IFrame;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }
    //新增会员
    public void add(Member member) {
         memberDao.add(member);
    }

    @Override
    public Map<String, Object> getMemberReport() {
        List<String> months = getMonthOfLastYear();
        List<Long> memberCount = new ArrayList<>();
        //统计每个月份对应的会员数量
        for (String month : months) {
            String firstDay = month +  "-01"; // 2020-01-01
            String lastDay = getThisMonthLastDay(firstDay);
            long count = memberDao.findCountBeforeByDate(lastDay);
            memberCount.add(count);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("months", months);
        map.put("memberCount", memberCount);
        return map;
    }

    @Override
    public Map<String, Object> getSetmealReportBySex() {
        List<Map<String,String>> list=memberDao.getSetmealReportBySex();
        List<String> count=new ArrayList<>();
        for (Map<String, String> stringIntegerMap : list) {
            if (stringIntegerMap.get("name").equals("0")){
                stringIntegerMap.put("name","未知性别");
                count.add("未知性别");
            }else if (stringIntegerMap.get("name").equals("1")){
                stringIntegerMap.put("name","男");
                count.add("男");
            }else{
                stringIntegerMap.put("name","女");
                count.add("女");
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("SexNames",count);
        map.put("SexCounts",list);
        return map;
    }

    @Override
    public Map<String, Object> getMemberByAge() {
        List<Member> members=memberDao.getMemberByAge();
        List<Map<String,String>> list = new ArrayList<>();
        int count1=0;
        int count2=0;
        int count3=0;
        int count4=0;
        int count5=0;
        for (Member member : members) {
            Date birthday = member.getBirthday();
            if (birthday==null){
                count5++;
            }else {
                long time = birthday.getTime();
                long now = System.currentTimeMillis();
                long l = now - time;
                int age= (int) (l/1000/60/60/24/365);
                if (age>=0&&age<18){
                    count1++;
                }else if (age>=18&&age<30){
                    count2++;
                }else if (age>=30&&age<45){
                    count3++;
                }else {
                    count4++;
                }
            }
        }
        Map<String, String> map1 = new HashMap<>();
        map1.put("name","0-18");
        map1.put("value",String.valueOf(count1));
        list.add(map1);
        Map<String, String> map2 = new HashMap<>();
        map2.put("name","18-30");
        map2.put("value",String.valueOf(count2));
        list.add(map2);
        Map<String, String> map3 = new HashMap<>();
        map3.put("name","30-45");
        map3.put("value",String.valueOf(count3));
        list.add(map3);
        Map<String, String> map4 = new HashMap<>();
        map4.put("name","45");
        map4.put("value",String.valueOf(count4));
        list.add(map4);
        Map<String, String> map5 = new HashMap<>();
        map5.put("name","未指定生日");
        map5.put("value",String.valueOf(count5));
        list.add(map5);

        List<String> agecount = new ArrayList<>();
        agecount.add("0-18");
        agecount.add("18-30");
        agecount.add("30-45");
        agecount.add("45");
        agecount.add("未指定生日");

        Map<String, Object> map = new HashMap<>();
        map.put("AgeCounts",list);
        map.put("agecount",agecount);
        return map;
    }


    @Override
    public Map getnewMemberReport(String dateStr1, String dateStr2) {
        List<String> months = getMonths(dateStr1,dateStr2);
        List<Long> memberCount = new ArrayList<>();
        for (String month : months) {
            String firstDay = month + "-01";
            String lastDay = getThisMonthLastDay(firstDay);
            long count = memberDao.findCountBeforeByDate(lastDay);
            memberCount.add(count);
        }
        Map map = new HashMap();
        map.put("months",months);
        map.put("memberCount",memberCount);
        return map;
    }

    private List<String> getMonths(String dateStr1, String dateStr2) {
        List<String> months = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            months = new ArrayList<>();
            Date date1 = sdf.parse(dateStr1);
            Date date2 = sdf.parse(dateStr2);
            long num1 = (date2.getTime()-date1.getTime())/1000/60/60/24/30;
            Calendar calendar = Calendar.getInstance();
            String cal = sdf.format(calendar.getTime());
            Date caldate = sdf.parse(cal);
            long num2 =(caldate.getTime()-date1.getTime())/1000/60/60/24/30;
            int num3 = (int) num2;
            calendar.add(Calendar.MONTH,-num3);
            for (int i = 0; i < num1 ; i++) {
                calendar.add(Calendar.MONTH,1);
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
                String date = sdf1.format(calendar.getTime());
                months.add(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return months;
    }


    /**
     * 获取最近一年的月份
     * @return
     */
    private List<String> getMonthOfLastYear(){
        List<String> months = new ArrayList<>();
        //获取前12个月份
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String date = simpleDateFormat.format(calendar.getTime());
            months.add(date);
        }
        return months;
    }
    /**
     * 根据这个月第一天获取这个月最后一天
     *
     */
    private String getThisMonthLastDay(String firstDay){
        //2020-02-01 thisMonthFirstDay
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date firstDate = sdf.parse(firstDay);
            //创建日历对象
            Calendar cal = Calendar.getInstance();
            //设置为本月一号
            cal.setTime(firstDate);
            //调整为下月1号
            cal.add(Calendar.MONTH, 1);
            //调整为前一天(本月的最后一天)
            cal.add(Calendar.DAY_OF_MONTH, -1);
            String str = sdf.format(cal.getTime());
            return str;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
